import pandas as pd
import numpy as np
PitchingData = pd.read_excel("PitchingData.xls")
PitchingData #data of each MLB pitcher's statistics in 2021
| Last Name | First Name | year | Age | AB | Hits | 1B | 2B | 3B | HR | SO | BattingAvg | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Wainwright | Adam | 2021 | 40 | 765 | 168 | 111 | 34 | 2 | 21 | 174 | 0.220 |
| 1 | Greinke | Zack | 2021 | 38 | 652 | 164 | 105 | 29 | 0 | 30 | 120 | 0.252 |
| 2 | Santana | Ervin | 2021 | 39 | 251 | 65 | 38 | 16 | 2 | 9 | 52 | 0.259 |
| 3 | Petit | Yusmeiro | 2021 | 37 | 296 | 69 | 47 | 8 | 2 | 12 | 37 | 0.233 |
| 4 | Jansen | Kenley | 2021 | 34 | 236 | 36 | 25 | 7 | 0 | 4 | 86 | 0.153 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 268 | Fleming | Josh | 2021 | 25 | 410 | 110 | 76 | 22 | 1 | 11 | 65 | 0.268 |
| 269 | Peacock | Matt | 2021 | 27 | 352 | 107 | 69 | 22 | 3 | 13 | 50 | 0.304 |
| 270 | Garcia | Luis | 2021 | 25 | 573 | 133 | 81 | 31 | 2 | 19 | 167 | 0.232 |
| 271 | Weathers | Ryan | 2021 | 22 | 362 | 101 | 62 | 19 | 0 | 20 | 72 | 0.279 |
| 272 | Gray | Josiah | 2021 | 24 | 265 | 63 | 32 | 10 | 2 | 19 | 76 | 0.238 |
273 rows × 12 columns
features = ["AB", "Hits", "1B", "2B", "3B", "HR","BattingAvg"]
target = "SO" #sets up our goal to predict strike outs
X = PitchingData[features]
Y = PitchingData[target]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2, random_state = 0)
from sklearn.neighbors import KNeighborsRegressor
knn = KNeighborsRegressor(n_neighbors=3)
knn
KNeighborsRegressor(n_neighbors=3)
knn.fit(X_train, Y_train)
KNeighborsRegressor(n_neighbors=3)
knn.score(X_train, Y_train), knn.score(X_test, Y_test)
(0.9161565642603654, 0.8398375528861681)
from IPython.display import Image #First Image Import. We will cut from here on out
Image("CorbinBurnesMound.jpeg")
#Corbin Burnes Season Batting Stats
CBurnes = {"AB": 612,
"H": 123,
"1B": 96,
"2B": 17,
"3B": 3,
"HR": 7,
"BattingAvg": 0.201}
CBurnesStats = [CBurnes["AB"], CBurnes["H"], CBurnes["1B"], CBurnes["2B"], CBurnes["3B"], CBurnes["HR"], CBurnes["BattingAvg"]]
CBurnesMatchup = [CBurnesStats]
CBurnesMatchup
[[612, 123, 96, 17, 3, 7, 0.201]]
Knn Prediction
Burnes = knn.predict(CBurnesMatchup)
Burnes
#Knn prediction for how many strikeouts Burnes will throw
array([187.66666667])
Linear Prediction
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr
LinearRegression()
lr.fit(X_train, Y_train)
lr.score(X_train, Y_train), lr.score(X_test, Y_test)
(0.8788696089129353, 0.8717657799643881)
Burnes = lr.predict(CBurnesMatchup)
Burnes
#Linear prediction for how many strike outs Burnes will throw
#More accurate to his actual number 223
array([192.75782211])
MLBData1 = pd.read_excel("MLBData1.xls")
MLBData1
#shows batting stats for each mlb player
| Name | Year | Age | AB | PA | Hits | 1B | 2B | 3B | HR | SO | BattingAvg | RBI | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Aaron Judge | 2021 | 29 | 550 | 633 | 158 | 95 | 24 | 0 | 39 | 158 | 0.287 | 98 |
| 1 | Bryce Harper | 2021 | 29 | 488 | 599 | 151 | 73 | 42 | 1 | 35 | 134 | 0.309 | 84 |
| 2 | Albert Pujols | 2021 | 41 | 275 | 296 | 65 | 45 | 3 | 0 | 17 | 45 | 0.236 | 50 |
| 3 | Miguel Cabrera | 2021 | 38 | 472 | 526 | 121 | 90 | 16 | 0 | 15 | 118 | 0.256 | 75 |
| 4 | Rene Rivera | 2021 | 38 | 69 | 78 | 16 | 11 | 3 | 0 | 2 | 29 | 0.232 | 9 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 551 | Wander Franco | 2021 | 20 | 281 | 308 | 81 | 51 | 18 | 5 | 7 | 37 | 0.288 | 39 |
| 552 | Jarren Duran | 2021 | 25 | 107 | 112 | 23 | 16 | 3 | 2 | 2 | 40 | 0.215 | 10 |
| 553 | Ryan Jeffers | 2021 | 24 | 267 | 293 | 53 | 28 | 10 | 1 | 14 | 108 | 0.199 | 35 |
| 554 | Owen Miller | 2021 | 25 | 191 | 202 | 39 | 27 | 8 | 0 | 4 | 54 | 0.204 | 18 |
| 555 | Andrew Vaughn | 2021 | 23 | 417 | 469 | 98 | 61 | 22 | 0 | 15 | 101 | 0.235 | 48 |
556 rows × 13 columns
#Batting Stats of all MLB players with interaction included
from ipywidgets import widgets, interactive, Layout
w_player = widgets.Dropdown(
description = 'player:',
options = ["All"] + sorted(list(set(MLBData1.Name))), # a sorted list of unique playerIDs plus All
value = "All",
style = {"description_width": '50px'},
layout = Layout(width="15%")
)
def view(player):
if player == "All":
df_tmp = MLBData1
else:
df_tmp = MLBData1[MLBData1.Name == player]
display(df_tmp)
title = "Batting Stats of Players".format(player)
df_tmp[["AB", "PA", "Hits", "1B", "2B", "3B", "HR", "SO", "BattingAvg", "RBI"]].plot(kind="bar", title=title, grid=True, figsize=(15,7))
i = interactive(view, player=w_player)
display(i)
features = ["AB", "PA", "Hits", "1B", "2B", "3B", "HR", "RBI","BattingAvg"]
target = "SO"
X = MLBData1[features]
Y = MLBData1[target]
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.5, random_state = 0)
from sklearn.neighbors import KNeighborsRegressor
knn = KNeighborsRegressor(n_neighbors=3)
knn
KNeighborsRegressor(n_neighbors=3)
knn.fit(X_train, Y_train)
knn.score(X_train, Y_train), knn.score(X_test, Y_test)
(0.8725679962838475, 0.8152481733672445)
# Next, we each chose a hitter to analyze
Image("JoseAltuveBatting.jpeg")
#Jose Altuve's Season Batting Stats
JAltuve = {"AB": 601,
"PA": 678,
"H": 167,
"1B": 103,
"2B": 32,
"3B": 1,
"HR": 31,
"RBI": 83,
"BattingAvg": 0.278}
JAltuveStats = [JAltuve["AB"], JAltuve["PA"], JAltuve["H"], JAltuve["1B"], JAltuve["2B"], JAltuve["3B"], JAltuve["HR"], JAltuve["RBI"], JAltuve["BattingAvg"]]
JAltuveMatchup = [JAltuveStats]
JAltuveMatchup
[[601, 678, 167, 103, 32, 1, 31, 83, 0.278]]
Knn Prediction
AltuveSO = knn.predict(JAltuveMatchup)
AltuveSO
array([145.66666667])
AltuveSO
#Knn prediction for how many times Altuve strikes out
array([145.66666667])
Linear Prediction
from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, Y_train)
lr.score(X_train, Y_train), lr.score(X_test, Y_test)
(0.8319171593242987, 0.8576345797150282)
lr.predict(JAltuveMatchup)
#Linear prediction for strike outs
array([135.47779906])
Image("BryceHarperBatting.jpeg")
#Bryce Harpers's Season Batting Stats
BHarper = {"AB": 488,
"PA": 599,
"H": 151,
"1B": 73,
"2B": 42,
"3B": 1,
"HR": 35,
"RBI": 84,
"BattingAvg": 0.309}
BHarperStats = [BHarper["AB"], BHarper["PA"], BHarper["H"], BHarper["1B"], BHarper["2B"], BHarper["3B"], BHarper["HR"], BHarper["RBI"], BHarper["BattingAvg"]]
BHarperMatchup = [BHarperStats]
BHarperMatchup
[[488, 599, 151, 73, 42, 1, 35, 84, 0.309]]
Knn Prediction
knn.predict(BHarperMatchup)
#knn prediction for strikeouts
array([116.33333333])
Linear Prediction
lr.predict(BHarperMatchup)
#linear strike out prediction
array([115.66207859])
Image("NolanArenadoBatting.jpeg")
#Nolan Arenado's Season Batting Stats
NArenado = {"AB": 593,
"PA": 653,
"H": 151,
"1B": 80,
"2B": 34,
"3B": 3,
"HR": 34,
"RBI": 105,
"BattingAvg": 0.255}
NArenadoStats = [NArenado["AB"], NArenado["PA"], NArenado["H"], NArenado["1B"], NArenado["2B"], NArenado["3B"], NArenado["HR"], NArenado["RBI"], NArenado["BattingAvg"]]
NArenadoMatchup = [NArenadoStats]
NArenadoMatchup
[[593, 653, 151, 80, 34, 3, 34, 105, 0.255]]
Knn Prediction
knn.predict(NArenadoMatchup)
#knn prediction
array([127.33333333])
Linear Prediction
lr.predict(NArenadoMatchup)
#linear prediction
array([148.23801846])
Image("AaronJudgeBatting.jpeg")
#Aaron Judge's Season Batting Stats
AJudge = {"AB": 550,
"PA": 633,
"H": 158,
"1B": 95,
"2B": 24,
"3B": 0,
"HR": 39,
"RBI": 98,
"BattingAvg": 0.287}
AJudgeStats = [AJudge["AB"], AJudge["PA"], AJudge["H"], AJudge["1B"], AJudge["2B"], AJudge["3B"], AJudge["HR"], AJudge["RBI"], AJudge["BattingAvg"]]
AJudgeMatchup = [AJudgeStats]
AJudgeMatchup
[[550, 633, 158, 95, 24, 0, 39, 98, 0.287]]
Knn Prediction
knn.predict(AJudgeMatchup)
#knn prediction for Judge
array([120.33333333])
Linear Prediction
lr.predict(AJudgeMatchup)
#linear prediction for Judge
array([134.76256019])
#Fastball stats for MLB Players of our choice
fastball = pd.read_excel("FastBall.xlsx")
fastball
| PlayerID | #Fastballs | %Fastballs | At Bats | FastballH | AvgFastballs | Fastball1B | Fastball2B | Fastball3B | FastballHR | FastballSO | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | J.Altuve | 1346 | 54.1 | 600 | 94 | 0.156667 | 56 | 17 | 1 | 20 | 34 |
| 1 | B.Harper | 1277 | 53.3 | 488 | 81 | 0.165984 | 36 | 22 | 1 | 22 | 62 |
| 2 | N.Arenado | 1385 | 56.8 | 591 | 74 | 0.125212 | 34 | 22 | 0 | 18 | 43 |
| 3 | A.Judge | 1345 | 51.1 | 545 | 94 | 0.172477 | 56 | 13 | 0 | 25 | 60 |
| 4 | C.Burnes | 1632 | 62.9 | 678 | 96 | 0.141593 | 73 | 14 | 3 | 6 | 134 |
#Bar Chart of batting stats vs. fastball pitch with interaction included
from ipywidgets import widgets, interactive, Layout
w_player = widgets.Dropdown(
description = 'player:',
options = ["All"] + sorted(list(set(fastball.PlayerID))), # a sorted list of unique playerIDs plus All
value = "All",
style = {"description_width": '50px'},
layout = Layout(width="15%")
)
def view(player):
if player == "All":
df_tmp = fastball
else:
df_tmp = fastball[fastball.PlayerID == player]
display(df_tmp)
title = "Batting Stats of Players vs Fastballs".format(player)
df_tmp[["Fastball1B", "Fastball2B", "Fastball3B", "FastballHR", "FastballSO"]].plot(kind="bar", title=title, grid=True, figsize=(15,7))
i = interactive(view, player=w_player)
display(i)
#Breakingball stats for MLB Players of our choice
breakingball = pd.read_excel("BreakingBall.xlsx")
breakingball
| PlayerID | #Breaking | %Breaking | At Bats | BreakingH | AvgBreaking | Breaking 1B | Breaking2B | Breaking3B | BreakingHR | BreakingSO | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | J.Altuve | 799 | 32.1 | 600 | 53 | 0.088333 | 34 | 12 | 0 | 7 | 41 |
| 1 | B.Harper | 734 | 30.6 | 488 | 38 | 0.077869 | 20 | 9 | 0 | 9 | 56 |
| 2 | N.Arenado | 856 | 35.1 | 591 | 53 | 0.089679 | 33 | 8 | 2 | 10 | 46 |
| 3 | A.Judge | 894 | 34.0 | 545 | 45 | 0.082569 | 29 | 9 | 0 | 7 | 60 |
| 4 | C.Burnes | 712 | 27.5 | 678 | 19 | 0.028024 | 16 | 2 | 0 | 1 | 84 |
#Bar Chart of batting stats vs. breakingball pitch with interaction included
from ipywidgets import widgets, interactive, Layout
w_player = widgets.Dropdown(
description = 'player:',
options = ["All"] + sorted(list(set(breakingball.PlayerID))), # a sorted list of unique playerIDs plus All
value = "All",
style = {"description_width": '50px'},
layout = Layout(width="15%")
)
def view(player):
if player == "All":
df_tmp = breakingball
else:
df_tmp = breakingball[breakingball.PlayerID == player]
display(df_tmp)
title = "Batting Stats of Players vs Fastballs".format(player)
df_tmp[["Breaking 1B", "Breaking2B", "Breaking3B", "BreakingHR", "BreakingSO"]].plot(kind="bar", title=title, grid=True, figsize=(15,7))
i = interactive(view, player=w_player)
display(i)
#Offspeed stats for MLB Players of our choice
offspeed = pd.read_excel("OffSpeed.xlsx")
offspeed
| PlayerID | #OffSpeed | %OffSpeed | At Bats | OffSpeedH | AvgOffspeed | Offspeed1B | OffSpeed2B | OffSpeed3B | OffSpeedHR | OffSpeedSO | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | J.Altuve | 341 | 13.7 | 600 | 19 | 0.031667 | 12 | 3 | 0 | 4 | 16 |
| 1 | B.Harper | 385 | 16.1 | 488 | 32 | 0.065574 | 17 | 11 | 0 | 4 | 16 |
| 2 | N.Arenado | 199 | 8.2 | 591 | 24 | 0.040609 | 13 | 4 | 1 | 6 | 6 |
| 3 | A.Judge | 393 | 14.9 | 545 | 17 | 0.031193 | 10 | 2 | 0 | 5 | 37 |
| 4 | C.Burnes | 250 | 9.6 | 678 | 8 | 0.011799 | 7 | 1 | 0 | 0 | 16 |
#Bar Chart of batting stats vs. offspeed pitch with interaction included
from ipywidgets import widgets, interactive, Layout
w_player = widgets.Dropdown(
description = 'player:',
options = ["All"] + sorted(list(set(offspeed.PlayerID))), # a sorted list of unique playerIDs plus All
value = "All",
style = {"description_width": '50px'},
layout = Layout(width="15%")
)
def view(player):
if player == "All":
df_tmp = offspeed
else:
df_tmp = offspeed[offspeed.PlayerID == player]
display(df_tmp)
title = "Batting Stats of Players vs Fastballs".format(player)
df_tmp[["Offspeed1B", "OffSpeed2B", "OffSpeed3B", "OffSpeedHR", "OffSpeedSO"]].plot(kind="bar", title=title, grid=True, figsize=(15,7))
i = interactive(view, player=w_player)
display(i)
#Our MLB players of choice and their batting stats for each pitching category
PitchTypeData = pd.read_excel("PitchTypeData.xlsx")
PitchTypeData
| PlayerID | PitchType | #Pitches | PercentPitch | At Bats | Hits | Average | 1B | 2B | 3B | HR | SO | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | J.Altuve | Fastball | 1346 | 54.1 | 600 | 94 | 0.156667 | 56 | 17 | 1 | 20 | 34 |
| 1 | B.Harper | Fastball | 1277 | 53.3 | 488 | 81 | 0.165984 | 36 | 22 | 1 | 22 | 62 |
| 2 | N.Arenado | Fastball | 1385 | 56.8 | 591 | 74 | 0.125212 | 34 | 22 | 0 | 18 | 43 |
| 3 | A.Judge | Fastball | 1345 | 51.1 | 545 | 94 | 0.172477 | 56 | 13 | 0 | 25 | 60 |
| 4 | C.Burnes | Fastball | 1632 | 62.9 | 678 | 96 | 0.141593 | 73 | 14 | 3 | 6 | 134 |
| 5 | J.Altuve | Offspeed | 341 | 13.7 | 600 | 19 | 0.031667 | 12 | 3 | 0 | 4 | 16 |
| 6 | B.Harper | Offspeed | 385 | 16.1 | 488 | 32 | 0.065574 | 17 | 11 | 0 | 4 | 16 |
| 7 | N.Arenado | Offspeed | 199 | 8.2 | 591 | 24 | 0.040609 | 13 | 4 | 1 | 6 | 6 |
| 8 | A.Judge | Offspeed | 393 | 14.9 | 545 | 17 | 0.031193 | 10 | 2 | 0 | 5 | 37 |
| 9 | C.Burnes | Offspeed | 250 | 9.6 | 678 | 8 | 0.011799 | 7 | 1 | 0 | 0 | 16 |
| 10 | J.Altuve | Breaking Ball | 799 | 32.1 | 600 | 53 | 0.088333 | 34 | 12 | 0 | 7 | 41 |
| 11 | B.Harper | Breaking Ball | 734 | 30.6 | 488 | 38 | 0.077869 | 20 | 9 | 0 | 9 | 56 |
| 12 | N.Arenado | Breaking Ball | 856 | 35.1 | 591 | 53 | 0.089679 | 33 | 8 | 2 | 10 | 46 |
| 13 | A.Judge | Breaking Ball | 894 | 34.0 | 545 | 45 | 0.082569 | 29 | 9 | 0 | 7 | 60 |
| 14 | C.Burnes | Breaking Ball | 712 | 27.5 | 678 | 19 | 0.028024 | 16 | 2 | 0 | 1 | 84 |
#Bar Chart of batting stats vs. pitch types with interaction included, data combined into one chart.
from ipywidgets import widgets, interactive, Layout
w_player = widgets.Dropdown(
description = 'player:',
options = ["All"] + sorted(list(set(PitchTypeData.PlayerID))), # a sorted list of unique playerIDs plus All
value = "All",
style = {"description_width": '50px'},
layout = Layout(width="15%")
)
def view(player):
if player == "All":
df_tmp = PitchTypeData
else:
df_tmp = PitchTypeData[PitchTypeData.PlayerID == player]
display(df_tmp)
title = "Batting Stats of Players vs Pitch Types".format(player)
df_tmp[["1B", "2B", "3B", "HR", "SO"]].plot(kind="bar", title=title, grid=True, figsize=(15,7))
i = interactive(view, player=w_player)
display(i)
#Basic pie chart format downloaded from matplotlib.org
#Percent of each Corbin Burnes pitch categories thrown
import matplotlib.pyplot as CBurnes
labels = 'Fastball', 'Breaking Ball', 'Offspeed'
sizes = [62.9, 27.5, 9.6]
explode = (0.1, 0, 0)
fig1, ax1 = CBurnes.subplots()
ax1.pie(sizes, explode = explode, labels = labels, autopct = '%1.1f%%',shadow = True, startangle = 90)
ax1.axis('equal')
CBurnes.show()
Pitch = {"1": "Fastball", "2": "BreakingBall", "3": "Offspeed"}
#Random selection of 1,2, or 3 / probabilities of each pitch are listed and the random choice selects with these in mind
Pitch1 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch1]
'Offspeed'
Pitch2 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch2]
'Fastball'
Pitch3 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch3]
'Fastball'
Pitch4 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch4]
'BreakingBall'
#Basic pie chart format downloaded from matplotlib.org
#Percent of each pitching category thrown to Jose Altuve
import matplotlib.pyplot as JAltuvePie
labels = 'Fastball', 'Breaking Ball', 'Offspeed'
sizes = [54.1, 32.1, 13.7]
explode = (0.1, 0, 0)
fig1, ax1 = JAltuvePie.subplots()
ax1.pie(sizes, explode = explode, labels = labels, autopct = '%1.1f%%',shadow = True, startangle = 90)
ax1.axis('equal')
JAltuvePie.show()
Next 3 cells are to demonstrate what kind of hit Altuve would get depending on what pitch was thrown from the above cells that predict Burnes's throw
#For each hitter, we took their hitting probabilities against each pitch and inserted them into another random choice function.
AltuveF = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.44, .13, .01, .16, .26])
AltuveB = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.36, .13, 0, .07, .44])
AltuveO = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.34, .09, .0, .11, .46])
Pitch5 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch5]
'BreakingBall'
AltuveB
'strike'
Pitch6 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch6]
'Fastball'
AltuveF
'single'
Image("JoseAltuveCelebrating.jpeg")
#Basic pie chart format downloaded from matplotlib.org
#Percent of each pitching category thrown to Bryce Harper
import matplotlib.pyplot as BHarperPie
labels = 'Fastball', 'Breaking Ball', 'Offspeed'
sizes = [53.3, 30.6, 16.1]
explode = (0.1, 0, 0)
fig1, ax1 = BHarperPie.subplots()
ax1.pie(sizes, explode = explode, labels = labels, autopct = '%1.1f%%',shadow = True, startangle = 90)
ax1.axis('equal')
BHarperPie.show()
We then went through and did the same thing for Harper and other batters following (as seen below)
HarperF = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.25, .15, .01, .15, .44])
HarperB = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.21, .10, 0, .10, .59])
HarperO = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.35, .23, 0, .08, .34])
Pitch7 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch7]
'BreakingBall'
HarperB
'strike'
Pitch8 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch8]
'Fastball'
HarperF
'single'
Image("BryceHarperCelebrating.jpeg")
#Basic pie chart format downloaded from matplotlib.org
#Percent of each pitching category thrown to Nolan Arenado
import matplotlib.pyplot as NArenadoPie
labels = 'Fastball', 'Breaking Ball', 'Offspeed'
sizes = [56.8, 35.1, 8.2]
explode = (0.1, 0, 0)
fig1, ax1 = NArenadoPie.subplots()
ax1.pie(sizes, explode = explode, labels = labels, autopct = '%1.1f%%',shadow = True, startangle = 90)
ax1.axis('equal')
NArenadoPie.show()
ArenadoF = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.29, .19, 0, .15, .37])
ArenadoB = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.33, .08, .02, .10, .47])
ArenadoO = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.43, .13, .04, .2, .2])
Pitch10 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch10]
'Fastball'
ArenadoF
'homerun'
Image("NolanArenadoCelebrating.jpeg")
#Basic pie chart format downloaded from matplotlib.org
#Percent of each pitching category thrown to Aaron Judge
import matplotlib.pyplot as AJudgePie
labels = 'Fastball', 'Breaking Ball', 'Offspeed'
sizes = [51.1, 34, 14.9]
explode = (0.1, 0, 0)
fig1, ax1 = AJudgePie.subplots()
ax1.pie(sizes, explode = explode, labels = labels, autopct = '%1.1f%%',shadow = True, startangle = 90)
ax1.axis('equal')
AJudgePie.show()
JudgeF = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.37, .08, 0, .16, .39])
JudgeB = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.28, .08, 0, .07, .57])
JudgeO = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'strike'], p = [.18, .04, 0, .09, .69])
Pitch11 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch11]
'Offspeed'
JudgeO
'strike'
Pitch12 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch12]
'Offspeed'
JudgeO
'strike'
Pitch13 = np.random.choice(a=["1", "2", "3"], p = [0.63, 0.27, 0.10])
Pitch[Pitch13]
'Fastball'
JudgeF3 = np.random.choice(a=['single', 'double', 'triple', 'homerun', 'Strike Out'], p = [.37, .08, 0, .16, .39])
JudgeF3 #This statement was made for the final pitch, changing strike to strikeout. Judge was the only player to reach this point.
'Strike Out'
Image("AaronJudgeSad.jpeg")
Image("CorbinBurnesCelebrating.jpeg")